package cn.tedu.encoding;

import sun.nio.cs.ext.GBK;

import java.io.*;
import java.util.Arrays;

/**本类用于测试编码转换流*/
public class TestEncode {
    public static void main(String[] args) {
//        f1();
        f2();
    }

    private static void f1() {
        OutputStreamWriter out=null;
        try {
            /**一定要保证保存和打开时使用的是同一张码表，就不会出现乱码的问题*/
//            out=new OutputStreamWriter(new FileOutputStream("1.txt"), "gbk");
//            out=new OutputStreamWriter(new FileOutputStream("1.txt"), "iso8859-1");
            out=new OutputStreamWriter(new FileOutputStream("1.txt"), "utf-8");
            out.write("海绵宝宝&派大星哈哈哈哈哈哈");
            System.out.println("执行成功");
        } catch (Exception e) {
            System.out.println("执行失败");
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private static void f2() {
        InputStreamReader in=null;
        try {
//            in=new InputStreamReader(new FileInputStream("1.txt"));
//            in=new InputStreamReader(new FileInputStream("1.txt"),"gbk");
            in=new InputStreamReader(new FileInputStream("1.txt"));
           char[] ch=new char[8192];
           System.out.println(Arrays.toString(ch));
           int len=in.read(ch);
           System.out.println(Arrays.toString(ch));
           System.out.println(len);
           System.out.println(new String(ch,0,len));
           System.out.println("恭喜您,读取成功!");
//            System.out.println(in.getEncoding());
//            System.out.println(in.read());
        } catch (Exception e) {
            System.out.println("很遗憾，读取失败");
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
